;(function ($) {

    $(function () {

        //
        var displayReferralList = [];
        var pageSize = 5;
        var totalPage = 0;
        var currentPage = 1;
        //获取分享用户列表
        function getReferrals(getReferralDetail, account) {
            account.ajax({
                type: 'GET',
                path: '/s/reference/referrals',
                success: function (result) {
                    getReferralDetail(result);
                },
                error: function (err) {
                    // alert(err.message);
                    //toastAlert(err,2);
                }
            });
        }


        var getReferralInfo = function getReferralInfo(referralList) {

            var idList = [];
            for (var i = 0; i < referralList.length; i++) {
                if ($.inArray(referralList[i].referralId, idList) === -1)
                    idList.push(referralList[i].referralId);
            }

            account.ajax({
                type: 'GET',
                path: '/s/students/socials',
                data: {ids: idList.toString()},
                success: function (result) {
                    showReferralList(referralList, result);
                },
                error: function (err) {

                }
            });
        }

        // 显示分享赠客列表
        function showReferralList(referralList, wechatInfoList) {

            var referralListHtml = '';

            for (var i = 0; i < wechatInfoList.length; i++) {

                var referral = {
                    short: false,
                    long: false,
                    total: 0,
                    nickName: '',
                    avatar: ''
                };

                for (var j = 0; j < referralList.length; j++) {
                    if (wechatInfoList[i].id === referralList[j].referralId) {
                        if (referralList[j].lessonCount === 1) {
                            referral.short = true;
                            referral.total += 1;
                        } else if (referralList[j].lessonCount === 10) {
                            referral.long = true;
                            referral.total += 10;
                        }
                    }
                }

                referral.nickName = wechatInfoList[i].nickname;
                referral.avatar = wechatInfoList[i].avatar;

                displayReferralList.push(referral);
                displayReferralList.push(referral);

            }


            var stopIndex =  pageSize >= displayReferralList.length ? displayReferralList.length:pageSize;
            var totalLesson = 0;
            for (var k = 0; k < stopIndex; k++) {
                referralListHtml += '<tr class="share-item">'+
                                    '<td class="atr"><img src="'+displayReferralList[k].avatar+'"><div class="nick-name">'+displayReferralList[k].nickName+'</div></td>'+
                                    '<td>'+ ((displayReferralList[k].short === true)?'<div class="tick-icon "></div>':'')+'</td>'+
                                    '<td>'+ ((displayReferralList[k].long === true)?'<div class="tick-icon "></div>':'')+'</td>'+
                                    '<td>'+displayReferralList[k].total+'</td>'+
                                    '</tr>';

                totalLesson += parseInt(displayReferralList[k].total);
            }

            $('#shareDetail').append(referralListHtml);

            $('#totalReferral').text(displayReferralList.length);
            $('#totalLesson').text(totalLesson);

            if(displayReferralList.length > 0) {

                $('#noShare').hide();
                $('#shareList').show();

                if( displayReferralList.length <= pageSize) {
                    $('#pagination').hide();
                } else {

                    $('.paging-counter .current').text(1);
                    if (displayReferralList.length % pageSize > 0) {
                        totalPage = parseInt(displayReferralList.length / pageSize) + 1;
                    } else {
                        totalPage = parseInt(displayReferralList.length / pageSize);
                    }
                    $('.paging-counter .total').text(totalPage);
                }
            } else {
                $('#noShare').show();
                $('#shareList').hide();
            }




        }

        //分页加载分享列表
        function refereshpagingList() {
            $('.share-item').remove();

            var startIndex = ((currentPage-1) * pageSize);
            var stopIndex = ( (currentPage  * pageSize) > displayReferralList.length)?displayReferralList.length:(currentPage  * pageSize);


            for (var i =startIndex ; i <stopIndex   ; i++) {
                $('#shareDetail').append(
                    '<tr class="share-item">' +
                    '<td class="atr"><img src="'+displayReferralList[i].avatar+'"><div class="nick-name">'+displayReferralList[i].nickName+'</div></td>'+
                    '<td>' + ((displayReferralList[i].short === true) ? '<div class="tick-icon "></div>' : '') + '</td>' +
                    '<td>' + ((displayReferralList[i].long === true) ? '<div class="tick-icon "></div>' : '') + '</td>' +
                    '<td>' + displayReferralList[i].total + '</td>' +
                    '</tr>'
                );
            }
        }


        var account = new Account();
        getReferrals(getReferralInfo, account);




        //分页导航
        $('.paging-left').on('touchstart',function(){
          $(this).addClass('paging-left-touch');
        });

        $('.paging-left').on('touchend',function(){
            $(this).removeClass('paging-left-touch');
        });

        $('.paging-right').on('touchstart',function(){
            $(this).addClass('paging-right-touch');
        });

        $('.paging-right').on('touchend',function(){
            $(this).removeClass('paging-right-touch');
        });

        $('.paging-left').click(function(){
            if(currentPage > 1) {
                currentPage--;
                $('.paging-counter .current').text(currentPage);
                if (currentPage === totalPage - 1) {
                    $('.paging-right').removeClass('dis-paging');
                }
                if (currentPage === 1) {
                    $('.paging-left').addClass('dis-paging');
                }
                refereshpagingList();
            }
        });


        $('.paging-right').click(function(){
            if(currentPage < totalPage) {
                currentPage++;
                $('.paging-counter .current').text(currentPage);
                if(currentPage === 2) {
                    $('.paging-left').removeClass('dis-paging');
                }
                if(currentPage === totalPage ) {
                    $('.paging-right').addClass('dis-paging');
                }
                refereshpagingList();
            }

        })

    })

})(window.Zepto);
